<script setup lang="ts">
import { getArticleDetailAPI, updateCollectAPI, updateLikeAPI, type Article } from '@/apis/article'
import { showSuccessToast, showToast } from 'vant'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'

// 获取路由
const route = useRoute()
// 文章详情
const article = ref<Article | null>(null)

// 获取面经详情
const getArticleDetail = async () => {
  try {
    // 获取面经详情API请求
    const res = await getArticleDetailAPI(route.params.id as string)
    // 赋值给文章详情
    article.value = res.data
  } catch (error) {
    console.error('获取文章详情失败:', error)
  }
}

// 调用获取面经详情
onMounted(() => {
  getArticleDetail()
})

const toggleLike = async () => {
  if (!article.value) return

  try {
    article.value.likeFlag = !article.value.likeFlag
    await updateLikeAPI(article.value.id)
    if (article.value.likeFlag) {
      article.value.likeCount++
      showSuccessToast('点赞成功')
    } else {
      article.value.likeCount--
      showToast('取消点赞')
    }
  } catch (error) {
    console.error('点赞操作失败:', error)
  }
}

const toggleCollect = async () => {
  if (!article.value) return

  try {
    article.value.collectFlag = !article.value.collectFlag
    await updateCollectAPI(article.value.id)
    if (article.value.collectFlag) {
      showSuccessToast('收藏成功')
    } else {
      showToast('取消收藏')
    }
  } catch (error) {
    console.error('收藏操作失败:', error)
  }
}
</script>

<template>
  <div class="article-view">
    <!-- 顶部导航栏 -->
    <van-nav-bar left-arrow left-text="返回" @click-left="$router.back()" title="面经详情" fixed />
    <!-- 文章详情头部 -->
    <van-skeleton v-if="!article?.stem" style="padding: 0">
      <template #template>
        <div style="padding: 20px 0; width: 100%">
          <van-skeleton-title />
          <van-skeleton-paragraph />
          <van-skeleton style="padding: 10px 0" title avatar :row="3" />
        </div>
      </template>
    </van-skeleton>

    <template v-else>
      <header class="header">
        <h1 class="header-title">{{ article.stem }}</h1>
        <p>{{ article.createdAt }} | {{ article.views }} 浏览量 | {{ article.likeCount }} 点赞数</p>
        <p>
          <!-- 头像 -->
          <img :src="`https://wsrv.nl/?url=${article.avatar}`" alt="" />
          <span>{{ article.creator }}</span>
        </p>
      </header>

      <main class="body">
        <div v-html="article.content"></div>
      </main>
      <div class="opt">
        <van-icon @click="toggleCollect" :class="{ active: article.collectFlag }" name="star-o" />
        <van-icon @click="toggleLike" :class="{ active: article.likeFlag }" name="like-o" />
      </div>
    </template>
  </div>
</template>

<style lang="scss" scoped>
.article-view {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
}
.header {
  width: 100%;

  .header-title {
    font-size: 24px;
  }
  p {
    color: #999;
    font-size: 12px;
    display: flex;
    align-items: center;
  }
  img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
  }
}
.opt {
  position: fixed;
  bottom: 100px;
  right: 0;
  > .van-icon {
    margin-right: 20px;
    background: #fff;
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 50%;
    box-shadow: 2px 2px 10px #ccc;
    font-size: 18px;
    &.active {
      background: #fec635;
      color: #fff;
    }
  }
}
</style>
